{extend  name="./app" /}

{block name="title"}添加句子{/block}

{block name="body"}
<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <h3>添加句子</h3>
           <div class="col-sm-12">
               <div class="alert alert-warning">
                   <p>1.请勿编写反动党，反动社会的语句。</p>
                   <p>2.请勿编写涉及黄赌毒的语句。</p>
               </div>
               <form action="" class="form-horizontal" method="post">
                   <input type="hidden" name="tag_ids" value="">
                   <div class="form-group">
                       <dl class="select-tag-box">
                           {volist name="categories" id="category"}
                           <dt>{$category->name}</dt>
                           <dd>
                               {volist name="$category->tags" id="tag"}
                               <span data-id="{$tag->id}" class="label label-default tag-item">{$tag->name}</span>
                               {/volist}
                           </dd>
                           {/volist}
                       </dl>
                   </div>
                   <div class="form-group">
                       <textarea name="sentence" class="form-control" rows="3" placeholder="请输入句子"></textarea>
                   </div>
                   <div class="form-group text-right">
                       <button type="submit" class="btn btn-primary">添加句子</button>
                   </div>
               </form>
           </div>
        </div>
    </div>
</div>
{/block}

{block name="js"}
<script>

    function removeItem($arr, $value)
    {
        $newArr = [];
        for (var i = 0; i < $arr.length; i++) {
            if ($arr[i] != $value) {
                $newArr.push($arr[i]);
            }
        }
        return $newArr;
    }

    $(function () {
        $('.tag-item').click(function () {
            var tagId = $(this).attr('data-id');
            var tagIds = $('input[name="tag_ids"]').val().split(',');
            if ($(this).hasClass('label-default')) {
                tagIds.push(tagId);
                $(this).removeClass('label-default').addClass('label-success');
            } else {
                tagIds = removeItem(tagIds, tagId)
                $(this).addClass('label-default').removeClass('label-success');
            }

            $('input[name="tag_ids"]').val(tagIds.join(','));
        });
    });
</script>
{/block}